import plugin from '../../lib/plugins/plugin.js'
import fs from "node:fs"

export class jiedian extends plugin {
    constructor() {
        super({
            name: 'syjs:更换节点',
            dsc: '切换更新面板使用的服务器',
            event: 'message',
            priority: 5000,
            rule: [
                {
                    reg: '^#更换(广州|上海|官方)节点$',// 食用方法 #更换上海节点   #更换广州节点   #更换官方节点
                    fnc: 'huan',
                    permission: 'master',
                },
            ]
        })
    }

    async huan(e) {
        try {
            let re = new RegExp("更换(.*?)节点")
            let url = new RegExp("url: '.*?'")
            let name = re.exec(e.msg)[1]
            let data = fs.readFileSync('./plugins/miao-plugin/config/profile_default.js', 'utf8')
            if (name == '广州') {
                data = data.replace(url, "url: 'https://enka.microgg.cn/'")
            }
            else if (name == '上海') {
                data = data.replace(url, "url: 'https://enka.minigg.cn/'")
            } else if (name == '官方') {
                data = data.replace(url, "url: 'https://enka.network/'")
            } else {
                return false
            }
            fs.writeFileSync('./plugins/miao-plugin/config/profile.js', data, (err) => {
                if (err) {
                    console.log('更换失败', err.message)
                    return false
                }
            })
            e.reply('更换节点完成，重启bot生效')
        } catch {
            e.reply('更换失败，检查自己是否修改过喵喵默认配置文件')
        }
    }
}
